#!/usr/bin/env python
#
# Copyright (C) 2011 Clement Lorteau <clem@lorteau.fr>	
#		
# This file is part of QuickScreenShots.		
# 	
# This program is free software; you can redistribute it and/or modify	
# it under the terms of the GNU General Public License as published by		
# the Free Software Foundation; version 3.	
# 	
# This program is distributed in the hope that it will be useful,		
# but WITHOUT ANY WARRANTY; without even the implied warranty of		
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the		
# GNU General Public License for more details.		
# 		
# You should have received a copy of the GNU General Public License		
# along with this program; if not, write to the Free Software		
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.

#to help py2app guess what's needed
import ctypes
import email

import os
import app
import sys
import platform
from __init__ import logfile, is_frozen


if __name__ == '__main__':
    try:
        os.unlink(logfile)
    except Exception, e:
        pass
    #sys.stderr = open(logfile, 'w')
    #print is_frozen
    """Under Windows, display unexpected exceptions in dialogs
       We use native api dialogs because the exception might have prevented the wx.App object to
       load"""
    if platform.system() == 'Windows':
		try:
			a = app.QSSApp(redirect=True, filename=logfile)
			a.MainLoop()
		except Exception, e:
			import win32ui, win32con
			win32ui.MessageBox("""The following unexpected error occured:
			%s
			
	Debug information was logged in %s.""" % (str(e), logfile),
							   'Sorry about that...',
							   win32con.MB_ICONERROR|win32con.MB_OK)
			raise e
    elif platform.system() == 'Darwin':
        """Under OS X, just output to the log file; the Console application makes it easy to read"""	
        a = app.QSSApp(redirect=True, filename=logfile)
        a.MainLoop()
    else:
        raise Exception("The platform %s is not supported" % platform.system())
    #sys.stderr.close()